const jsonfile = require('jsonfile');
const moment = require('moment');
const {sortBy,isEmpty}=require('loadsh')
const logFun={
    setLog(username,input,output,listObject){
        const today = moment().format('YYYY_MM_DD');
        const create_time = moment().format('YYYY-MM-DD HH:mm:ss');
        if(!listObject){
            listObject=jsonfile.readFileSync(`data/order/${today}.json`)
        }
        if(!listObject[username].logs){
            listObject[username].logs=[{input,output,create_time}]
        }else {
            listObject[username].logs.unshift({input,output,create_time})
        }
        jsonfile.writeFileSync(`data/order/${today}.json`,listObject)
    },
    getLog(username,createTime){
        const today = createTime || moment().format('YYYY_MM_DD');
        const listObject=jsonfile.readFileSync(`data/${today}.json`)
        if(!username) return sortBy(Object.values(listObject).map(i=>i.logs),'time')
        return listObject[username].logs
    }
}
module.exports=logFun